package com.cloud.class04.controller;

import brave.http.HttpServerRequest;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.cloud.class04.entities.Pay;
import com.cloud.class04.resp.ResultData;
import com.cloud.class04.service.PayService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.Enumeration;

@RestController
public class PayGateWayController {
    @Resource
    PayService payService;

    @GetMapping(value = "/pay/gateway/get/{id}")
    public ResultData<Pay> getById(@PathVariable("id") Integer id) {
        Pay pay = payService.getById(id);
        return ResultData.success(pay);
    }

    @GetMapping(value = "/pay/gateway/info")
    public ResultData<String> getGatewayInfo() {
        return ResultData.success("gateway info test：" + IdUtil.simpleUUID());
    }


    @GetMapping(value = "/pay/gateway/filter")
    public ResultData<String> getGatewayFilter(HttpServletRequest request) {
//        long l = System.currentTimeMillis();

        String result = "";
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String headerName = headerNames.nextElement();
            String headerValue = request.getHeader(headerName);
            System.out.println("请求头名为： " + headerName + "---" + "请求头值为： " + headerValue);

            if (headerName.equalsIgnoreCase("X-Request-class04")
                    || headerName.equalsIgnoreCase("X-Request-class05")) {
                result = result + headerName + " : " + headerValue + " ";

            }
        }
        System.out.println("********************************************");
        String class04 = request.getParameter("class04");
        System.out.println("class04 : " + class04);
        String class05 = request.getParameter("class05");
        System.out.println("class05 : " + class05);
        System.out.println("********************************************");
//        long lj = System.currentTimeMillis();
//        System.out.println("耗时： " + (lj - l));
        return ResultData.success("getGatewayFilter 过滤器 test： " + result + " \t " + DateUtil.now());


    }

}


 